﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Storage.Core.MediaItem
{
    /// <summary>
    /// 
    /// </summary>
    public static class HttpPostedFileExtension
    {
        /// <summary>
        /// png,jpg,gif,bmp
        /// </summary>
        private static string[] img_exArr = new string[] { "255216", "7173", "6677", "13780" };

        /// <summary>
        /// 判断文件是否是图片
        /// </summary>
        /// <param name="postedFile"></param>
        /// <returns></returns>
        public static bool isImage(this HttpPostedFileBase postedFile)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                postedFile.InputStream.Position = 0;
                postedFile.InputStream.CopyTo(ms);
                ms.Position = 0;
                postedFile.InputStream.Position = 0;
                using (BinaryReader r = new BinaryReader(ms))
                {
                    string fileclass = "";
                    byte buffer;
                    buffer = r.ReadByte();
                    fileclass = buffer.ToString();
                    buffer = r.ReadByte();
                    fileclass += buffer.ToString();
                    return img_exArr.Any(o => o.Contains(fileclass.ToLower()));
                }
            }
        }

        /// <summary>
        /// 获取文件的哈希SHA1值
        /// </summary>
        /// <param name="postedFile"></param>
        /// <returns></returns>
        public static string getSHA1(this HttpPostedFileBase postedFile)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                postedFile.InputStream.Position = 0;
                postedFile.InputStream.CopyTo(ms);
                ms.Position = 0;
                postedFile.InputStream.Position = 0;
                SHA1 sha1 = new SHA1CryptoServiceProvider();
                byte[] retval = sha1.ComputeHash(ms);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retval.Length; i++)
                    sb.AppendFormat("{0:X2}", retval[i]);
                return sb.ToString();
            }
        }
        /// <summary>
        /// 文件大小超出范围
        /// </summary>
        /// <param name="postedFile"></param>
        /// <param name="length">字节大小 例 100kb，100*1024 = 102400 </param>
        /// <returns></returns>
        public static bool isBigSize(this HttpPostedFileBase postedFile, int length)
        {
            return postedFile.ContentLength > length;
        }

        /// <summary>
        /// 创建保存文件
        /// </summary>
        /// <param name="postedFile"></param>
        /// <param name="imageStorage"></param>
        /// <param name="virtualPath">虚拟相对路径/xxx/xxx</param>
        /// <returns></returns>
        public static string CreateImagePathFromStream(this HttpPostedFileBase postedFile, IMediaItemStorage imageStorage, string virtualPath)
        {
            var imagePath = string.Empty;
            if (postedFile != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    postedFile.InputStream.Position = 0;
                    postedFile.InputStream.CopyTo(memoryStream);
                    postedFile.InputStream.Position = 0;
                    imagePath = imageStorage.Storage(memoryStream, virtualPath, Guid.NewGuid().ToString() + Path.GetExtension(postedFile.FileName));
                }
            }
            return imagePath;
        }

        





    }
}
